<?php

class Application_Model_Posts {

    public function getPostList() {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $stmt = $dbAdapter->query('CALL getAllPost()');
            $arrResult = $stmt->fetchAll();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        return $arrResult;
    }

    public function getDetailPost($id) {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $stmt = $dbAdapter->prepare("CALL getDetailPost(?)");
            $stmt->bindParam(1, $id);
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        return $arrResult;
    }

    public function createPost($arrPost) {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $dbAdapter->insert('tbl_post', $arrPost);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function deletePost($id) {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $dbAdapter->delete('tbl_post', 'id=' . $id);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function editPost($id, $arrPost) {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $dbAdapter->update("tbl_post", $arrPost, "id='$id'");
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function getTop($arrTopID) {
        try {
            $dbAdapter = Zend_Registry::get('dbAdapter');
            $strTopID=implode(",",$arrTopID);
            $stmt = $dbAdapter->query("SELECT * FROM tbl_post WHERE id IN ($strTopID)");
            $result = $stmt->fetchAll();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        return $result;
    }

}
